app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
  /*证书清单*/
  .state('certificate', {
    url: '/certificate',
    templateUrl: 'app/partials/certificate/index.html',
    controller: function($scope,$location,Certificate) {
      $scope.widget_title = '证书类型管理';
      $scope.certificates = Certificate.query();
      /* 删除 记录*/
      $scope.itemDelete = function(item){
        Certificate.delete({},{"id": $scope.certificates[item].id})
        $scope.certificates.splice(item, 1);
      }
      $scope.itemEdit = function(item){
        $location.url("/certificate/edit/"+item)
      }
   }
  })

  /*添加证书*/
  .state('certificate.new', {
    url: '/new',
    templateUrl: 'app/partials/certificate/form.html',
    controller: function($scope,$state,Certificate) {
      $scope.widget_title = '添加类型证书';
      $scope.update = function(certificate){
        Certificate.save(certificate,function(nr){
          $scope.certificates.push(nr["result"]);
          $scope.certificate = {};
          $state.go('certificate');
        },function(){
          alert("填写有误，请检查输入项目")
        });


      }
   }
  })

  /* 编辑证书 */
  .state('certificate.edit', {
    url: '/edit/:id',
    templateUrl: 'app/partials/certificate/form.html',
    controller: function($scope,$state,$stateParams,$location,Certificate) {
      $scope.widget_title = '编辑证书';
      $scope.certificate = Certificate.get({id: $stateParams.id});
      $scope.update = function(certificate){
        Certificate.update(certificate,function(){
          $state.transitionTo('certificate',$stateParams,{reload: true});
        },function(){
          alert("填写有误，请检查输入项目")
        });

      }
   }
  })

  /*--------------证书提醒---------------*/
  .state('certificate_notify', {
    url: '/certificate_notify',
    templateUrl: 'app/partials/certificate/notify.html',
    controller: function($scope,$location, PositionCertificateStandard, EmployeeCertificate, Certificate, $state, $stateParams) {
      $scope.widget_title = '证书提醒';
      $scope.list_will_valid_certificates = EmployeeCertificate.list_will_valid_certificates();
      $scope.list_valid_certificates      = EmployeeCertificate.list_valid_certificates();
      $scope.certificates = Certificate.query();

      $scope.display = function(certificates){
        return $.map(certificates, function(certificate){ return certificate.name }).join();
      }

      $scope.display_modal = function(id){
        EmployeeCertificate.get({id: id},function(nr){
          $scope.employee_certificate = nr
        });
        $('#myModal').modal('toggle');
      }

      $scope.update = function(employee_certificate){
        EmployeeCertificate.update(employee_certificate, function(nr){
          $state.transitionTo('certificate_notify',$stateParams,{reload: true});
        }, function(nr){

        });
      }
   }
  })
  /*--------------证书过期检索---------------*/
  .state('query_valid_certificates', {
    url: '/query_valid_certificates',
    templateUrl: 'app/partials/certificate/query_valid_certificates.html',
    controller: function($scope,$location, PositionCertificateStandard, EmployeeCertificate, Certificate, $state, $stateParams) {
      $scope.widget_title = '证书过期检索';

      $scope.search = function(employee_certificate){
        $scope.list_valid_certificates = EmployeeCertificate.query_will_certificates(employee_certificate);
      }

   }
  })
  /*--------------证书缺失提醒---------------*/
  .state('certificate_lost', {
    url: '/certificate_lost',
    templateUrl: 'app/partials/certificate/lost.html',
    controller: function($scope,$location, PositionCertificateStandard, EmployeeCertificate) {
      $scope.widget_title = '证书提醒';
      $scope.list_lost_certificates       = PositionCertificateStandard.list_lost_certificate();

      $scope.display = function(certificates){
        return $.map(certificates, function(certificate){ return certificate.name }).join();
      }
   }
  })
  /*--------------持证情况分析---------------*/
  .state('certificate_analysis', {
    url: '/certificate_analysis',
    templateUrl: 'app/partials/certificate/analysis.html',
    controller: function($scope,$location, PositionCertificateStandard, EmployeeCertificate, Certificate) {
      $scope.widget_title = '持证率分析';
      $scope.certificates = Certificate.analysis();

      $scope.display = function(datas){
        return $.map(datas, function(data){ return data.name }).join();
      }
   }
  })
})
